package com.company.mediaplayer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MusicService extends Service {

    public static final String MUSIC_PATH = "/mnt/sdcard/Music";// 找到music存放的路径。
    private File file ;//MP3 文件的文件夹
    public String [] mfilename;//mp3清单
    public MediaPlayer player; // 定义多媒体对象
    public int songNum; // 当前播放的歌曲在List中的下标
    public int songaction;//歌曲当前执行操作
    //private boolean jump =true ;//歌曲是否跳转


    public  int getSongNum(){
        return  songNum;
    }

    public MusicService() {
        player = new MediaPlayer();
        mfilename =Index.getMfilename();
        /*file =new File(MUSIC_PATH);


        mfilename= file.list(new MusicFilter());
        for (int i=0 ;i<file.list(new MusicFilter()).length;i++){
            Log.i("msg",mfilename[i]);
        }*/
    }



    public class Mbinder extends Binder{
        public MediaPlayer getMbinder(){
            return player;
        }
    }

    @Override
    public void onCreate() {

        super.onCreate();
        Log.i("msg", "MusicService onCreat... ");



    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        songNum = intent.getIntExtra("songnum",0);
        songaction =intent.getIntExtra("music", 0);


        switch (songaction){
            case Index.PLAY:
                start();
                break;
            case  Index.NEXT:
                next();
                break;
            case  Index.PAUSE:
                pause();
                break;
            case  Index.PREVIOUS:
                previous();
                break;
        }

        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return  new Mbinder();
    }

    //播放状态
    public void start() {
        try {
            //if(jump) {
                player.reset(); //重置多媒体
                player.setDataSource(MUSIC_PATH + "/" + mfilename[songNum]);//为多媒体对象设置播放路径
                Log.i("msg", MUSIC_PATH + "/" + mfilename[songNum]);
                player.prepare();//准备播放
                //jump=false;
            //}
                player.start();//开始播放

          /*  //setOnCompletionListener 当当前多媒体对象播放完成时发生的事件.
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer arg0) {
                    next();//歌曲播放完毕,自动播放下一首.
                }
            });*/
        } catch (Exception e) {
            Log.v("MusicService", e.getMessage());
        }
    }

    public void next() {
        //if (songNum<=(mfilename.length-1))
            //songNum = songNum + 1;
        //else
        if (songNum==mfilename.length-1)
            Log.i("msg","已经是最后一首");
        //jump =true;
        start();
    }

    public void previous() {
       /* if (songNum>=1)
            songNum = songNum - 1;
        else*/
        if (songNum==0)
            Log.i("msg","已经是第一首");
        //jump =true;
        start();
    }

    public void pause() {
        if (player.isPlaying())
            player.pause();
        else

            player.start();
        }

    public void stop() {
        if (player.isPlaying()) {
            player.stop();
        }
    }

}
